/*
 * @Description: 档案管理权限规则 - 创建规则弹窗reducer
 * @Author: Rfan
 * @Date: 2022-04-06 13:13:27
 * @LastEditTime: 2022-04-07 11:15:57
 */

import { insertItemAfter } from '@/utils/list';
import {
  ADD_CONDITION_ITEM,
  CLEAR_CONDITION_LIST,
  DELETE_LAST_CONDITION_ITEM,
  HIDE_DIALOG,
  SHOW_DIALOG
} from '../constants';

export const AddRuleReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 添加条件条目
     */
    case SHOW_DIALOG:
      return {
        ...state,
        visible: true
      };

    /**
     * @description: 添加条件条目
     */
    case HIDE_DIALOG:
      return {
        ...state,
        visible: false
      };

    /**
     * @description: 添加条件条目
     * @param {*} 要添加的条目
     */
    case ADD_CONDITION_ITEM:
      return {
        ...state,
        conditionList: insertItemAfter(action.data, [...state.conditionList])
      };

    /**
     * @description: 删除最后一条条目
     */
    case DELETE_LAST_CONDITION_ITEM: {
      const _list = [...state.conditionList];
      _list.pop();
      return {
        ...state,
        conditionList: _list.map((item) => item)
      };
    }

    /**
     * @description: 清空所有条件
     */
    case CLEAR_CONDITION_LIST:
      return {
        ...state,
        conditionList: []
      };

    default:
      console.log('untouched', action);
      return state;
  }
};
